﻿namespace QiangPiao
{
    using QiangPiao.Properties;
    using SQ.Base;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Net;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using QiangPiao.Browser;

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1301:AvoidDuplicateAccelerators")]
    public class FrmMain : Form
    {
        private IContainer components = null;
        private FrmYuDing frm;
        private FrmPerson frmp;
        public HttpHelper http = new HttpHelper();
        private FrmLogin login;
        private ToolStripMenuItem mbtnLogin;
        private ToolStripMenuItem mbtnReg;
        private MenuStrip menuStrip1;
        private Panel panel1;
        public Regex regbed_level_order_num = new Regex("<input type=\"hidden\" name=\"orderRequest.bed_level_order_num\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regcancel_flag = new Regex("<input type=\"hidden\" name=\"orderRequest.cancel_flag\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regend_time = new Regex("<input type=\"hidden\" name=\"orderRequest.end_time\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regfrom_station_name = new Regex("<input type=\"hidden\" name=\"orderRequest.from_station_name\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regfrom_station_telecode = new Regex("<input type=\"hidden\" name=\"orderRequest.from_station_telecode\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regid_mode = new Regex("<input type=\"hidden\" name=\"orderRequest.id_mode\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regseat_type_code = new Regex("<input type=\"hidden\" name=\"orderRequest.seat_type_code\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regstart_time = new Regex("<input type=\"hidden\" name=\"orderRequest.start_time\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regstation_train_code = new Regex("<input type=\"hidden\" name=\"orderRequest.station_train_code\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regticket_type_order_num = new Regex("<input type=\"hidden\" name=\"orderRequest.ticket_type_order_num\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regto_station_name = new Regex("<input type=\"hidden\" name=\"orderRequest.to_station_name\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regto_station_telecode = new Regex("<input type=\"hidden\" name=\"orderRequest.to_station_telecode\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regTOKEN = new Regex("<input type=\"hidden\" name=\"org.apache.struts.taglib.html.TOKEN\" value=\"([^\"]*)\">", RegexOptions.Compiled);
        public Regex regtrain_date = new Regex("<input type=\"hidden\" name=\"orderRequest.train_date\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regtrain_no = new Regex("<input type=\"hidden\" name=\"orderRequest.train_no\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex rexCheckCode = new Regex("<span id=\"randErr\">", RegexOptions.Compiled);
        public Regex rexEx = new Regex("var message = \"(.+)\"", RegexOptions.Compiled);
        public Regex rexEx2 = new Regex("<span id=\"randErr\"> <img\\s+src='images/er/tools_ico2.gif' width='16' height='16' /><span\\s+style=\"color: red;\">([^<]+)</span> </span>", RegexOptions.Compiled);
        public Regex rexFun = new Regex(@"javascript:getSelected\('([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#(\d+)'\)");
        public Regex rexLogin = new Regex(@"isLogin\s*=\s*true", RegexOptions.Compiled);
        public Regex rexlst = new Regex(@"(\d+),<span [^>]+>([^<]+)</span>,((<img[^>]+>)*)([^<]+)<br>([^,]+),((<img[^>]+>)*)([^<]+)<br>([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^\\]+)");
        public Regex rexMang = new Regex("确认客票的状态后再尝试后续操作。谢谢！", RegexOptions.Compiled);
        public Regex rexStation = new Regex(@"@([^|]+)\|([^|]+)\|([^|]+)\|(\d+)", RegexOptions.Compiled);
        public Regex rexSu = new Regex("id=\"reChooseButton\"", RegexOptions.Compiled);
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel tlbl;
        private ToolStripStatusLabel tlblNum;
        private ToolStripStatusLabel tlblType;
        private ToolStripStatusLabel toolStripStatusLabel1;
        private ToolStripStatusLabel toolStripStatusLabel2;
        private ToolStripStatusLabel toolStripStatusLabel3;
        private ToolStripStatusLabel toolStripStatusLabel4;
        private TextBox txtMsg;
        private Splitter splitter1;
        private ToolStrip toolStrip;
        private ToolStripButton newWindowToolStripButton;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripButton closeToolStripButton;
        private TabControl tabControl;
        private TabPage tbpQiangpiao;
        private ToolStripStatusLabel toolStripStatusLabel5;


        /// <summary>
        /// 选项卡管理类
        /// </summary>
        private WindowManager _windowManager;

        /// <summary>
        /// 得到选项卡管理对象
        /// </summary>
        public WindowManager WindowManager
        {
            get { return _windowManager; }
        }


        public FrmMain()
        {
            this.InitializeComponent();

            _windowManager = new WindowManager(this.tabControl);
           // _windowManager.StatusTextChanged += new EventHandler<TextChangedEventArgs>(_windowManager_StatusTextChanged);

            _windowManager.New();//添加一个新的web页
            _windowManager.Close();
            //tabControl.SelectedIndex = 0;//选中第一个选项卡
            this.ChangeTxt();
            this.frm = new FrmYuDing(this);
            this.frm.Dock = DockStyle.Fill;
            this.frm.Location = new Point(3, 3);
            this.frm.Name = "frmYuDing1";
            this.frm.Size = new Size(0x2ba, 0x10a);
            this.frm.TabIndex = 0;
            this.tabControl.SelectedTab.Controls.Add(this.frm);
            this.frmp = new FrmPerson();
        }

        public void ChangeState(string state)
        {
            if (base.InvokeRequired)
            {
                base.Invoke(new dlgWriteInfo(this.ChangeState), new object[] { state });
            }
            else
            {
                this.tlbl.Text = state;
            }
        }

        public void ChangeTxt()
        {
            //this.tlblNum.Text = PublicInfo.AuthorizeInfo.AuthorizeTimes.ToString();
            //this.tlblType.Text = (PublicInfo.AuthorizeInfo.ForceTimes > 1) ? "可使用暴力模式" : "不可使用暴力模式";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }


        //更新状态栏
        void _windowManager_StatusTextChanged(object sender, TextChangedEventArgs e)
        {
            this.toolStripStatusLabel5.Text = e.Text;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
           
        }

        private void GetStationName()
        {
            string hTML = this.http.GetHTML("https://dynamic.12306.cn/otsweb/js/common/station_name.js?version=2.0", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            this.WriteHtml(hTML);
            foreach (Match match in this.rexStation.Matches(hTML))
            {
                this.frm.dataSetPiaoInfo1.DTCityCode.AddDTCityCodeRow(match.Groups[3].Value, match.Groups[2].Value, match.Groups[1].Value, match.Groups[4].Value);
            }
            this.frm.dataSetPiaoInfo1.FillData();
            this.frm.dataSetPiaoInfo1.WriteXml(AppDomain.CurrentDomain.BaseDirectory + "ChePiaoData.xml");
            this.frmp.dataSetPiaoInfo1.FillData();
            this.frmp.dataSetPiaoInfo1.WriteXml(AppDomain.CurrentDomain.BaseDirectory + "ChePiaoData.xml");
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain));
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.mbtnLogin = new System.Windows.Forms.ToolStripMenuItem();
            this.mbtnReg = new System.Windows.Forms.ToolStripMenuItem();
            this.panel1 = new System.Windows.Forms.Panel();
            this.txtMsg = new System.Windows.Forms.TextBox();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.toolStripStatusLabel5 = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.tlblNum = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStripStatusLabel3 = new System.Windows.Forms.ToolStripStatusLabel();
            this.tlblType = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStripStatusLabel2 = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStripStatusLabel4 = new System.Windows.Forms.ToolStripStatusLabel();
            this.tlbl = new System.Windows.Forms.ToolStripStatusLabel();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.toolStrip = new System.Windows.Forms.ToolStrip();
            this.newWindowToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.closeToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.tabControl = new System.Windows.Forms.TabControl();
            this.tbpQiangpiao = new System.Windows.Forms.TabPage();
            this.menuStrip1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.toolStrip.SuspendLayout();
            this.tabControl.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mbtnLogin,
            this.mbtnReg});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(712, 25);
            this.menuStrip1.TabIndex = 1;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // mbtnLogin
            // 
            this.mbtnLogin.Name = "mbtnLogin";
            this.mbtnLogin.Size = new System.Drawing.Size(44, 21);
            this.mbtnLogin.Text = "登录";
            this.mbtnLogin.Click += new System.EventHandler(this.mbtnLogin_Click);
            // 
            // mbtnReg
            // 
            this.mbtnReg.Name = "mbtnReg";
            this.mbtnReg.Size = new System.Drawing.Size(68, 21);
            this.mbtnReg.Text = "人员信息";
            this.mbtnReg.Click += new System.EventHandler(this.mbtnReg_Click);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.txtMsg);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 331);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(712, 119);
            this.panel1.TabIndex = 2;
            // 
            // txtMsg
            // 
            this.txtMsg.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtMsg.Location = new System.Drawing.Point(0, 0);
            this.txtMsg.Multiline = true;
            this.txtMsg.Name = "txtMsg";
            this.txtMsg.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtMsg.Size = new System.Drawing.Size(712, 119);
            this.txtMsg.TabIndex = 0;
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripStatusLabel5,
            this.toolStripStatusLabel1,
            this.tlblNum,
            this.toolStripStatusLabel3,
            this.tlblType,
            this.toolStripStatusLabel2,
            this.toolStripStatusLabel4,
            this.tlbl});
            this.statusStrip1.Location = new System.Drawing.Point(0, 450);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(712, 22);
            this.statusStrip1.TabIndex = 5;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // toolStripStatusLabel5
            // 
            this.toolStripStatusLabel5.Name = "toolStripStatusLabel5";
            this.toolStripStatusLabel5.Size = new System.Drawing.Size(0, 17);
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(465, 17);
            this.toolStripStatusLabel1.Spring = true;
            this.toolStripStatusLabel1.Text = "当日可预定车票数量:";
            this.toolStripStatusLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tlblNum
            // 
            this.tlblNum.Name = "tlblNum";
            this.tlblNum.Size = new System.Drawing.Size(15, 17);
            this.tlblNum.Text = "0";
            // 
            // toolStripStatusLabel3
            // 
            this.toolStripStatusLabel3.Name = "toolStripStatusLabel3";
            this.toolStripStatusLabel3.Size = new System.Drawing.Size(11, 17);
            this.toolStripStatusLabel3.Text = "|";
            // 
            // tlblType
            // 
            this.tlblType.Name = "tlblType";
            this.tlblType.Size = new System.Drawing.Size(104, 17);
            this.tlblType.Text = "不可使用暴力模式";
            // 
            // toolStripStatusLabel2
            // 
            this.toolStripStatusLabel2.Name = "toolStripStatusLabel2";
            this.toolStripStatusLabel2.Size = new System.Drawing.Size(11, 17);
            this.toolStripStatusLabel2.Text = "|";
            // 
            // toolStripStatusLabel4
            // 
            this.toolStripStatusLabel4.Name = "toolStripStatusLabel4";
            this.toolStripStatusLabel4.Size = new System.Drawing.Size(59, 17);
            this.toolStripStatusLabel4.Text = "当前状态:";
            // 
            // tlbl
            // 
            this.tlbl.Name = "tlbl";
            this.tlbl.Size = new System.Drawing.Size(32, 17);
            this.tlbl.Text = "待命";
            // 
            // splitter1
            // 
            this.splitter1.BackColor = System.Drawing.SystemColors.ActiveBorder;
            this.splitter1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.splitter1.Location = new System.Drawing.Point(0, 321);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(712, 10);
            this.splitter1.TabIndex = 4;
            this.splitter1.TabStop = false;
            // 
            // toolStrip
            // 
            this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newWindowToolStripButton,
            this.toolStripSeparator3,
            this.closeToolStripButton});
            this.toolStrip.Location = new System.Drawing.Point(0, 25);
            this.toolStrip.Name = "toolStrip";
            this.toolStrip.Size = new System.Drawing.Size(712, 25);
            this.toolStrip.TabIndex = 7;
            this.toolStrip.Text = "toolStrip1";
            // 
            // newWindowToolStripButton
            // 
            this.newWindowToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.newWindowToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("newWindowToolStripButton.Image")));
            this.newWindowToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.newWindowToolStripButton.Name = "newWindowToolStripButton";
            this.newWindowToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.newWindowToolStripButton.Text = "新建选项卡";
            this.newWindowToolStripButton.ToolTipText = "新建选项卡";
            this.newWindowToolStripButton.Click += new System.EventHandler(this.newWindowToolStripButton_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // closeToolStripButton
            // 
            this.closeToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.closeToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("closeToolStripButton.Image")));
            this.closeToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.closeToolStripButton.Name = "closeToolStripButton";
            this.closeToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.closeToolStripButton.Text = "关闭选项卡";
            this.closeToolStripButton.ToolTipText = "关闭选项卡";
            this.closeToolStripButton.Click += new System.EventHandler(this.closeToolStripButton_Click);
            // 
            // tabControl
            // 
            this.tabControl.Controls.Add(this.tbpQiangpiao);
            this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl.Location = new System.Drawing.Point(0, 50);
            this.tabControl.Name = "tabControl";
            this.tabControl.SelectedIndex = 0;
            this.tabControl.ShowToolTips = true;
            this.tabControl.Size = new System.Drawing.Size(712, 271);
            this.tabControl.TabIndex = 8;
            this.tabControl.Visible = false;
            // 
            // tbpQiangpiao
            // 
            this.tbpQiangpiao.Location = new System.Drawing.Point(4, 22);
            this.tbpQiangpiao.Name = "tbpQiangpiao";
            this.tbpQiangpiao.Padding = new System.Windows.Forms.Padding(3);
            this.tbpQiangpiao.Size = new System.Drawing.Size(704, 245);
            this.tbpQiangpiao.TabIndex = 0;
            this.tbpQiangpiao.Text = "抢票";
            this.tbpQiangpiao.UseVisualStyleBackColor = true;
            // 
            // FrmMain
            // 
            this.ClientSize = new System.Drawing.Size(712, 472);
            this.Controls.Add(this.tabControl);
            this.Controls.Add(this.toolStrip);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.menuStrip1);
            this.Controls.Add(this.statusStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "FrmMain";
            this.Text = "抢票工具";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.FrmMain_Load);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.toolStrip.ResumeLayout(false);
            this.toolStrip.PerformLayout();
            this.tabControl.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        [DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
        private void mbtnLogin_Click(object sender, EventArgs e)
        {
            if ((this.login == null) || this.login.IsClosed)
            {
                this.login = new FrmLogin(this);
                this.login.Show();
            }
            else
            {
                this.login.Activate();
            }
        }

        private void mbtnReg_Click(object sender, EventArgs e)
        {
            frmp.ShowDialog();
            //new FrmCheckAuthorize().ShowDialog();
        }

        public void Navigate(string url)
        {
           // _windowManager.Close();
            ExtendedWebBrowser brw = _windowManager.New(false);
            brw.Navigate(url);
        }

        public void NavigateMain()
        {
            string uriString = "https://dynamic.12306.cn/otsweb/main.jsp";
            CookieCollection cookieCollection = this.http.GetCookieCollection(new Uri(uriString));
            InternetSetCookie("https://dynamic.12306.cn/otsweb/", "JSESSIONID", cookieCollection["JSESSIONID"].Value);
            InternetSetCookie("https://dynamic.12306.cn/", "BIGipServerotsweb", cookieCollection["BIGipServerotsweb"].Value);
            this.Navigate(uriString);
        }

        public void ReadData()
        {
            this.frm.dataSetPiaoInfo1.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "ChePiaoData.xml");
            this.frmp.dataSetPiaoInfo1.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "ChePiaoData.xml");
        }

        public void WriteHtml(string str)
        {
            if (Settings.Default.IsDebug)
            {
                Log.WriteLog4(str, LOGTYPE.DEBUG);
            }
        }

        public void WriteInfo(string str)
        {
            if (this.txtMsg.InvokeRequired)
            {
                this.txtMsg.Invoke(new dlgWriteInfo(this.WriteInfo), new object[] { str });
            }
            else
            {
                this.txtMsg.Text = this.txtMsg.Text + DateTime.Now.ToString("HH:mm:ss: ") + str + "\r\n";
                this.txtMsg.SelectionStart = this.txtMsg.Text.Length - 1;
                this.txtMsg.ScrollToCaret();
                if (Settings.Default.IsDebug)
                {
                    Log.WriteLog4(str, LOGTYPE.DEBUG);
                }
            }
        }

        public bool IsLogin { get; set; }

        private delegate void dlgWriteInfo(string str);

        private void newWindowToolStripButton_Click(object sender, EventArgs e)
        {
            this._windowManager.New();
        }

        private void closeToolStripButton_Click(object sender, EventArgs e)
        {
            this._windowManager.Close();
        }
    }
}

